package com.example.famhealing.init;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

import com.example.famhealing.R;
import com.example.famhealing.R.id;
import com.example.famhealing.R.layout;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class JoinActivity extends Activity implements OnClickListener {
	private EditText name = null;
	private EditText phone = null;
	private FragmentActivity fa;

	private TextView mDateDisplay;
	private int mYear;
	private int mMonth;
	private int mDay;
	static final int DATE_DIALOG_ID = 0;

	static final int CALL_CAMERA = 0;
	static final int CALL_CAMERA2 = 1;
	static final int CALL_GALLERY = 2;
	String mPath;
	private Uri mImageCaptureUri;
	private ImageView mPhotoImageView;
	private Button mButton,mSendButton;
	private LinearLayout heightWeight;
	ArrayAdapter<CharSequence> adspin;
	boolean mInitSpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_join);

		name = (EditText)findViewById(R.id.username);
		phone = (EditText)findViewById(R.id.phonenum);
		heightWeight = (LinearLayout)findViewById(R.id.heigthWeight);
		RadioGroup group = (RadioGroup)findViewById(R.id.radiogroup);
		group.setOnCheckedChangeListener(mRadioCheck);
		heightWeight.setVisibility(View.INVISIBLE);

		mPhotoImageView = (ImageView) findViewById(R.id.image);
		mPhotoImageView.setOnClickListener(this);

		mDateDisplay = (TextView) findViewById(R.id.borndate);
		mDateDisplay.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth= c.get(Calendar.MONTH);
		mDay  = c.get(Calendar.DAY_OF_MONTH);
		updateDisplay();

		Spinner spin = (Spinner)findViewById(R.id.phonespinner);
		spin.setPrompt("번호 선택");

		adspin = ArrayAdapter.createFromResource(this, R.array.phone, 
				android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);

		spin.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, 
					int position, long id) {
			}
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case DATE_DIALOG_ID : return new DatePickerDialog(this,mDateSetListener,mYear,mMonth,mDay);
		}

		return null;
	}
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth=monthOfYear;
			mDay=dayOfMonth;
			updateDisplay();

		}
	};

	private void updateDisplay() {
		mDateDisplay.setText(
				new StringBuilder()
				.append(mYear).append("년 ")
				.append(mMonth+1).append("월 ")
				.append(mDay).append("일 생")
				);

	}

	RadioGroup.OnCheckedChangeListener mRadioCheck = 
			new RadioGroup.OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if (group.getId() == R.id.radiogroup) {
				switch (checkedId) {
				case R.id.radio1:
					heightWeight.setVisibility(View.VISIBLE);
					break;
				case R.id.radio2:
					heightWeight.setVisibility(View.INVISIBLE);
					break;
				}
			}
		}
	};

	public void onClick(View v) {
		String user_name = name.getText().toString();
		String phone_num = phone.getText().toString();

		DialogInterface.OnClickListener cameraListener = new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				doTakePhotoAction();
				
			}

		};

		DialogInterface.OnClickListener albumListener = new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				doTakeAlbumAction();
			}
		};

		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		};

		switch(v.getId()) {
		case R.id.okbtn:
			String phonenum = phone.getText().toString();
			phonenum = phonenum.substring(0, 3) + "-" + phonenum.substring(3, 7) + "-" + phonenum.substring(7);
		
			new AlertDialog.Builder(this)
			.setTitle("전화번호 인증")
			.setMessage(phonenum + "\n\n입력하신 전화번호가 정확한가요?\n\n위 전화번호로 SMS를 통해 인증번호를 보내드립니다. 통신사 사정에 따라 SMS전송에 다소 시간이 걸릴 수 있습니다." +
					"\n\n전화번호를 수정하시려면 취소를 선택해주세요.\n\n")
					.setPositiveButton("확인", mClick)
					.setNegativeButton("취소", mClick)
					.show();

			break;
		case R.id.image:
			new AlertDialog.Builder(this).
			setTitle("업로드할 이미지 선택").
			setPositiveButton("사진촬영", cameraListener).
			setNeutralButton("앨범선택", albumListener).
			setNegativeButton("취소", cancelListener)
			.show();
			break;
		}
	}

	DialogInterface.OnClickListener mClick = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			if (whichButton == DialogInterface.BUTTON_POSITIVE) {
				Intent intent = new Intent(JoinActivity.this, AuthCodeActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				finish();
			}
		}
	};


	//카메라에서 이미지 가져오기
	private void doTakePhotoAction()
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, CALL_CAMERA);
		

	}

	//앨범에서 이미지 가져오기

	private void doTakeAlbumAction()
	{
		// 앨범 호출
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
		startActivityForResult(intent, CALL_GALLERY);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode != RESULT_OK)
		{
			return;
		}
		switch(requestCode)
		{
		case CALL_CAMERA:
			Bitmap bmp=(Bitmap)data.getExtras().get("data"); 
			int width=150;
			int height=150;
			Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
			mPhotoImageView.setImageBitmap(resizedbitmap);
			break;
		case CALL_GALLERY:
			try {
				Bitmap bmp2=MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
				int width2=150;
				int height2=150;
				Bitmap resizedbitmap2=Bitmap.createScaledBitmap(bmp2, width2, height2, true); 
				mPhotoImageView.setImageBitmap(resizedbitmap2);
			} catch (Exception e) { ; }
			break;
		}
	}


	public void setImage(){
		mPhotoImageView.setImageURI(mImageCaptureUri);
	}
}

